package cn.shenlong.portal.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.Event;
import cn.shenlong.portal.domain.EventImg;
import cn.shenlong.portal.mapper.EventImgMapper;
import cn.shenlong.portal.mapper.EventMapper;
import cn.shenlong.portal.service.EventService;


@Service("eventService")
public class EventServiceImpl implements EventService {

	@Autowired
	private EventMapper eventMapper;
	
	@Autowired
	private EventImgMapper eventImgMapper;
	
	
	@Override
	public Page<Event> findPageByParm(Map<String, Object> otherParm,
			Integer pageNo, Integer pageSize) {
		Page<Event> p = new Page<Event>();
		List<Event> eventList = eventMapper.findPageListByCriteria(
				otherParm,
				initRowBounds(pageSize, pageNo, p));
		Long count = eventMapper.findCountByCriteria( otherParm);
		p.setList(eventList);
		p.setTotalCount(count.intValue());
		return p;
	}
	
	
	

	@Override
	public Event findByPk(Integer eventId) {
		return eventMapper.findByPK(eventId);
	}

	@Override
	public Integer save(Event event) {
		event.setCreatedt(new Date());
		event.setUpdatedt(new Date());
		eventMapper.saveEntity(event);
		return event.getId();
 
	}

	@Override
	public void update(Event event) {
		event.setUpdatedt(new Date());
		eventMapper.updateEntity(event);
		
	}

	@Override
	public void deleteByIds(Integer[] ids) {
		eventMapper.deleteByPKArray(ids);
		
	}

	@Override
	public List<Event> findByList(Map<String, Object> otherParm, Integer cout) {
		List<Event> eventList = eventMapper.findPageListByCriteria(
				otherParm,
				initRowBounds(cout, 0, new Page()));
 
		return eventList;
	}
	
	private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Page p) {
		RowBounds rowBounds = null;
		if (pageSize == null || pageNo == null) {
			rowBounds = new RowBounds();
		} else {
			p.setPageNo(pageNo);
			p.setPageSize(pageSize);
			rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
		}
		return rowBounds;
	}




	@Override
	public List<EventImg> findImgs(Map<String, Object> otherParm,
			Integer count) {
		if(count ==null){
			return eventImgMapper.findPageListByCriteria(otherParm, new RowBounds());
		}else{
			return eventImgMapper.findPageListByCriteria(otherParm, initRowBounds(count, 0, new Page()));
		}	
		
 
	}




	@Override
	public void saveImgs(EventImg eventImg) {
		eventImgMapper.saveEntity(eventImg);
		
	}
	@Override
	public void saveImgs(String[] url, String[] desc, Integer eventId) {
		if(url==null){
			return;
		}
		 for (int i = 0; i < url.length; i++) {
			 if(url[i]!=null){
				 EventImg img = new EventImg();
				 img.setEventId(eventId);
				 img.setUrl(url[i]);
				 if(desc!=null&&desc[i]!=null){
					 img.setImgDesc(desc[i]);
				 }
				 
				 eventImgMapper.saveEntity(img);
			 }
		}
		
	}



	@Override
	public void deleteImgs(Integer[] id) {
		eventImgMapper.deleteByPKArray(id);
		
	}




	@Override
	public void deleteByEventId(Integer eventId) {
		eventImgMapper.deleteByEventId(eventId);
		
	}





}
